package com.cl.short_video_java.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.short_video_java.pojo.VideoTag;
import com.cl.short_video_java.pojo.VideoType;
import com.cl.short_video_java.service.VideoTagService;
import com.cl.short_video_java.service.VideoTypeService;
import com.cl.short_video_java.mapper.VideoTypeMapper;
import com.cl.short_video_java.util.StringUtils;
import com.cl.short_video_java.vo.TypeSearch;
import com.cl.short_video_java.vo.type.Type;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author object
 * @description 针对表【video_type】的数据库操作Service实现
 * @createDate 2024-10-31 15:58:59
 */
@Service
public class VideoTypeServiceImpl extends ServiceImpl<VideoTypeMapper, VideoType>
		implements VideoTypeService {

	@Resource
	private VideoTypeMapper videoTypeMapper;
	@Autowired
	private VideoTagService videoTagService;

	@Override
	public Boolean InsertVideoType(String name) {
		ArrayList<VideoType> videoTypes = new ArrayList<>();
		String[] parts = name.split(",");
		for (String part : parts) {
			VideoType videoType = new VideoType();
			videoType.setName(part);
			videoType.setCreateTime(LocalDateTime.now());
			videoTypes.add(videoType);
		}
		boolean saveBatch = this.saveBatch(videoTypes);
		return saveBatch;


	}


	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean RemoveVideoTypeService(List<Integer> ids) {
		for (Integer id : ids) {
			videoTagService.remove(new QueryWrapper<VideoTag>().eq("type_id", id));
		}
		boolean b = this.removeBatchByIds(ids);
		return b;
	}

	@Override
	public PageInfo<VideoType> getPage(Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<VideoType> videoTypes = videoTypeMapper.selectList(null);
		PageInfo<VideoType> videoTypePageInfo = new PageInfo<>(videoTypes);
		return videoTypePageInfo;
	}

	@Override
	public Page<VideoType> getPage(TypeSearch typeSearch) {
		Page<VideoType> page = this.page(new Page<>(typeSearch.getPageNum(), typeSearch.getPageSize()), new QueryWrapper<VideoType>()
				.like(StringUtils.isNotEmpty(typeSearch.getName()), "name", typeSearch.getName())
				.orderByDesc("create_time"));
		for (VideoType videoType : page.getRecords()) {
			Page<VideoTag> tagPage = videoTagService.page(new Page<>(1, 100), new QueryWrapper<VideoTag>().eq("type_id", videoType.getId()));
			videoType.setList(tagPage.getRecords());
		}
		return page;
	}

	@Override
	public void save(Type type) {
		long count = this.count(new QueryWrapper<VideoType>().like("name", type.getName()));
		Assert.isTrue(count == 0, "当前分类已存在");
		VideoType videoType = new VideoType();
		if (type.getId() != null) {
			videoType.setId(type.getId());
		}
		videoType.setName(type.getName());
		videoType.setCreateTime(LocalDateTime.now());
		this.saveOrUpdate(videoType);
	}

    @Override
    public List<VideoType> typePageService() {
		List<VideoType> list = this.list();
		return list;
    }
}




